'''
Created on 16 de Nov de 2011

@author: 33120 37176
'''

import pygame
from pygame.locals import *
import random

GRID_SIZE = (11,11)     #patches
PATCH_SIZE = (32,18)    #pixels/patch

COLOR_OFF = (200,200,200)
COLOR_ON = (255,0,0)

w = GRID_SIZE[0] * PATCH_SIZE[0]
h = GRID_SIZE[1] * PATCH_SIZE[1]

pygame.init()
SCREEN = pygame.display.set_mode((w,h))

clock = pygame.time.Clock()

class Patch:
    def __init__(self,x,y):
        self.pxcor = x
        self.pycor = y
        self.rect = Rect(x*PATCH_SIZE[0],y*PATCH_SIZE[1],PATCH_SIZE[0],PATCH_SIZE[1])
        self.state = False
        pygame.draw.rect(SCREEN,COLOR_OFF,self.rect,3)
        
    def setstate(self,state):
        self.state = state
        if self.state:
            pygame.draw.rect(SCREEN,COLOR_ON,self.rect,3)
        else:
            pygame.draw.rect(SCREEN,COLOR_OFF,self.rect,3)
        
    def getstate(self):
        return self.state
    
class Grid2D:
    def __init__(self,ncols,nrows):
        self.grid = []
        for y in range(nrows):
            for x in range(ncols):
                self.grid.append(Patch(x,y))
                
    def getPatch(self,x,y):
        return self.grid[x+y*GRID_SIZE[0]]
                
g = Grid2D(*GRID_SIZE)

while True:
    for event in pygame.event.get():
        print event
        if event.type == QUIT:
            exit()
            
    #SCREEN.fill((100,100,100))
    
    #p = random.choice(g.grid)
    
    #x = random.randint(0,GRID_SIZE[0]-1)
    #y = random.randint(0,GRID_SIZE[1]-1)
    #p = g.getPatch(x,y)
    p = g.getPatch(2,3)
    p.setstate(True)
    #p.setstate(random.choice((False,True)))
       
    time_passed = clock.tick(5)
    
    pygame.display.flip()
    
